/** 流程待办详情展示 */
var app = getApp()
var utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   // commonUrl: '',
    commonUrl: app.GLOBAL_CONFIG.commonUrl,
    procDefName:'',
    applyUserName:'',
    assignee:'',
    detailList:[],//当前instance内容
    authUrl:'',//审批url
    actInfo:null,//Act信息
    isAuthFinish:false,//是否审批完成
    primaryId:0,//当前instance 实例的主键ID
    itemDetails:[],//审批对象详情清单
    authFlag:0, //审批意见（1同意，0不同意）
    readyToShow:false,
    rowsAttachmentUrl:[],
    rowsAttachment:[],
    isProblemSolution:false,//是否是协调事项管理
    probCreateUser:'',//协调事项管理创建人id
  },

  /**
   * 生命周期函数--监听页面加载
   */ 
  onLoad: function (obj) {
    var that = this
    that.setData({
      procDefName: obj.procDefName,
      taskName: obj.taskName,
      applyUserName: obj.applyUserName,
      taskCreateDate: obj.taskCreateDate,
      taskId: obj.taskId,
      taskDefKey: obj.taskDefKey,
      procInsId: obj.procInsId,
      procDefId: obj.procDefId, 
      status: obj.status,
      assignee: obj.assignee
    })
   
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.navigateBack({
        delta: 1
      })
      wx.showToast({
        title: '网络错误请检查',
        duration: 2000,
      })
    } else {
      var url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.taskDetail + "?taskId=" + that.data.taskId + "&taskName=" + that.data.taskName + "&taskDefKey=" + that.data.taskDefKey + "&procInsId=" + that.data.procInsId + "&procDefId=" + that.data.procDefId + "&status=" + that.data.status,
        cookie = app.globalData.cookie;  
      utils.getRequest(url, cookie,app,that, that.successOper,function(res){
        wx.navigateBack({
          delta: 1
        })
        wx.showToast({
          title: '获取数据失败，请稍后重试',
          image: '../../images/fail.png',
          duration: 2000
        })
      })
    }
  },

  successOper:function(res,that){
    if (res.data.operateMessage != '' && res.data.operateMessage != undefined) {
      wx.showModal({
        title: '提示',
        content: res.data.operateMessage,
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
    } else if ((res.data.operateMessage == '' || res.data.operateMessage == undefined) && (res.data.rows == null || res.data.rows == undefined || res.data.rows == '')) {
      wx.navigateBack({
        delta: 1
      })
      wx.showToast({
        title: '获取数据失败，请稍后重试',
        image: '../../images/fail.png',
        duration: 2000
      })

    } else {//成功
      var attachment = res.data.rowsAttachment
      if (attachment != undefined || attachment != null) { 
        var attachType = attachment.type,//类型
          attaceResour = attachment.resources,//附件详细信息
          attachUrls = [];
        if (attachType == "projectDocument") {//项目类型文件
          attachUrls = utils.getProjectAttacheUrls(attaceResour, app)
          that.setData({
            attachType: attachType,
            rowsAttachment: attaceResour,
            rowsAttachmentUrl: attachUrls
          })
        } else if (attachType == "commonDocument") {//一般类型文件
          attachUrls = utils.getAttacheUrls(attaceResour, app)
          that.setData({
            attachType: attachType,
            rowsAttachment: attaceResour,
            rowsAttachmentUrl: attachUrls
          }) 
        }
      }
      if (res.data.isProblemSolution == "true" && (res.data.createUser != null || res.data.createUser != undefined)) {
        that.setData({
          isProblemSolution: true,
          probCreateUser: res.data.createUser + ''
        })
      }

      that.setData({
        readyToShow: true,
        detailList: res.data.rows,
        authUrl: res.data.authUrl,
        actInfo: res.data.actInfo[0],
        primaryId: res.data.primaryId,
        itemDetails: res.data.itemDetails,
      })
    }
  },

  radioChange: function(e){
    console.log("radio value "+ e.detail.value)
    this.setData({
      authFlag:e.detail.value
    })

  },
/**
 * 
 * 待办任务逻辑：点击详情时，从后台获取信息，并判断taskurl是否为空，为空则使用通用模板，否则使用该url，在后台该url方法内做处理，回传具体业务相应的字段信息
 */
  formSubmit:function(e){
    var that = this,
    comment = e.detail.value.comment;
    if (comment == null || comment == '') {
      wx.showToast({
        title: '请填写意见',
        image: '../../images/warning.png',
        duration: 2000
      })
      this.setData({
        focus: true
      })
    } else {
      var url = app.GLOBAL_CONFIG.commonUrl + that.data.authUrl + '?isWxApp=true&taskId=' + that.data.actInfo.taskId + '&taskDefKey=' + that.data.actInfo.taskDefKey + '&procInsId=' + that.data.actInfo.procInsId + '&procDefId=' + that.data.actInfo.procDefId + '&businessId=' + that.data.actInfo.businessId + '&businessTable=' + that.data.actInfo.businessTable + '&assignee=' + that.data.assignee + '&id=' + that.data.primaryId + '&comment=' + comment;
      if (that.data.isProblemSolution == true){
        if (that.data.probCreateUser == that.data.assignee){
          that.setData({ authFlag: 1 })
        }
        url = url + '&flag=' + that.data.authFlag + '&createUser=' + that.data.probCreateUser
      }else{
        url = url + '&flag=' + that.data.authFlag
      }
      
      var cookie = app.globalData.cookie;
      if (utils.checkNetWorkStatus() == false) {
        console.log('网络错误');
        wx.showToast({
          title: '网络错误请检查',
          duration: 2000,
        })
      } else {
        utils.postRequest(url, cookie, app, that,function (res,that) {
          if (res.data == "success") {
            that.setData({
              isAuthFinish: true
            })
          } else {
            wx.showToast({
              title: '操作失败请重试',
              image: '../../images/fail.png',
              duration: 2000
            })
          }
        },function(res){
          wx.showToast({
            title: '操作失败请重试',
            image: '../../images/fail.png',
            duration: 2000
          })
        })
      }
   
  }

  },
  chooseOtherUser: function () {
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {
      var that = this
      wx.navigateTo({
        url: '../contacts/contacts' + '?flag=' + that.data.authFlag + '&taskId=' + that.data.actInfo.taskId + '&taskDefKey=' + that.data.actInfo.taskDefKey + '&procInsId=' + that.data.actInfo.procInsId + '&procDefId=' + that.data.actInfo.procDefId + '&businessId=' + that.data.actInfo.businessId + '&businessTable=' + that.data.actInfo.businessTable + '&id=' + that.data.primaryId + '&authUrl=' + that.data.authUrl,
      })
    }
  
  },
  //TODO 将基础信息附件及讨论详情分别加上id并通过id来区分二者，分别对的预览路径做整理，最后调用预览函数实现预览
  //http://www.wxapp-union.com/forum.php?mod=viewthread&tid=2426 文件操作demo
  viewAttachment: function (e) {
    var that = this,
     fileType = e.target.dataset.type,
     current = app.GLOBAL_CONFIG.commonUrl + e.target.dataset.src; 
    fileType = fileType.toLocaleLowerCase()
    switch (fileType) {
      /** 
       * 图片格式
       */
      case '.png':
        utils.imagePreview(current, that.data.rowsAttachmentUrl);
        break;
      case '.jpg':
        utils.imagePreview(current, that.data.rowsAttachmentUrl);
        break;
      case '.bmp':
        utils.imagePreview(current, that.data.rowsAttachmentUrl);
        break;
      case '.jpeg':
        utils.imagePreview(current, that.data.rowsAttachmentUrl);
        break;
      /**
       * 其他格式
       暂时屏蔽，若需要下载，再实现
       */
      case '.pdf':
        utils.documentOperate(current);
        break;
      case '.docx'://暂时java
        utils.documentOperate(current);
        break;
      case '.doc'://暂时java
        utils.documentOperate(current);
        break;
      case '.xlsx':
        utils.documentOperate(current);
        break;
      case '.xls':
        utils.documentOperate(current);
        break;
      case '.pptx':
        utils.documentOperate(current);
        break;
      case '.ppt':
        utils.documentOperate(current);
        break;
      default:
        wx.showToast({
          title: '不支持的文件类型',
          image: '../../images/warning.png'
        })
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成 
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   // this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})